<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>可可文章管理系统</title>
<link href="../template/css/ke.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="http://img.kekenet.com/statics/js/jquery.min.js"></script>
</head>
<body>

<!-- main -->
<div class="issCont">

  <div class="SelectTab">
    <h1 class="SelectTab_title">选择栏目：</h1>
   	<select name="catid" size="12">
	  <option value="0" selected="selected">不限栏目</option>
	  {{foreach from=$sub_cats item=c1}}
	    <option disabled="disabled" >{{$c1['catname']}}</option>
           {{foreach from=$c1['child'] item=c2}}
		  <option disabled="disabled" >->{{$c2['catname']}}</option>
             {{foreach from=$c2['child'] item=c3}}
		    <option cans="1" value="{{$c3['catid']}}" >--->{{$c3['catname']}}</option>
	      {{/foreach}}
           {{/foreach}}
	  {{/foreach}}
	  </select>
  </div>
  
  <div>
  	<input type="button" value="开始生成" onclick="javascript: do_make();" />
  </div>
  <hr />
</div>

<div id="output"></div>

<script>
var make_signle = function(id, fn){
	$.getJSON('../index.php/detail.index?do=makestatic&id=' + id, function(data) {
      if (data.status == 1) {
          // var cnt = parseInt($('#maked_cnt').html());
          // $('#maked_cnt').html(++cnt);
          // $('#output_info #list').prepend('　　' + data.id + ' 生成成功.<br />');

          if (data.pages.length > 0) {
        	  	var m = data.pages.length - 1;
              $(data.pages).each(function(k, pg) {
            	  $.getJSON('../index.php/detail.index?do=makestatic&id=' + id + '&page=' + pg, function(data) {
                  	  m--;
                  	  if(m<=0)
                  		  fn(1);
                  });
              });
          } else {
        	  fn(1);
          }
      } else {
    	  fn(0);
      }
  });

};
var make = function(pid, cid, catid, title, fn){
	var ids = [];
	var no = 0;
	function _make() {
	  if(ids.length <= 0) {
		  console.log(ids.length, catid);
		  $('#st_' + catid).html('finish.');
		  fn();
		  fn = function(){};
		  return ;
	  }
	  var id = ids.pop();
	  no++;

  	$('#no_' + catid).html(no);
	  make_signle(id, _make);
	}
	
  $('#output').prepend('<div>开始生成['+title+'] <span id="no_'+catid+'">0</span>/<span id="max_'+catid+'">1</span> <span style="color:red;" id="st_'+catid+'">makeing....</span></div>');
  $.getJSON('../index.php/category.get_index?catid=' + catid, function(data) {
	  ids = data.ids;
	  $('#max_' + data.catid).html(data.ids.length);

	  _make();
  });
};
var all_index = 0;
var all_make = function(){
	console.log('call! all_make');
	var p = $($('select[name=catid] option[cans=1]')[all_index]);
	make(p.attr('data-pid'), p.attr('data-cid'), p.attr('value'), p.html(), function(){
		all_index++;
		all_make();
	});
};
var do_make = function(){
	var sel_id = $('select[name=catid]').val();
	if(sel_id == 0) {
		all_make();
	} else {
		var p = $('select[name=catid]').find('option[value='+ sel_id +']');
		make(p.attr('data-pid'), p.attr('data-cid'), sel_id, p.html(), function(){
			
		});
	}
};
</script>

</body>
</html>